var a = require("../../../utils/httpPost.js"), t = require("../../../component/microPage/common/data/index.js"), 
app = getApp();

Page({
  data: {
    userId: 0,
    menuId: 34,
    title: "",
    isLoading: true,
    loading:false,
    pageIndex:1,
    pageSize:20,
    sections:[],
    currentIndex: 0,
    banners:[],
    searchValue:'',
    isiphonex: app.globalData.isiphonex,
    isxiaomi: app.globalData.isxiaomi,
    serviceList:[],
    goodsList:[],
    typeId:0,
  },
  onLoad: function (options) {
    console.log('smallPage onLoad options',options)
    if(options!=undefined &&options.id){
      this.data.menuId = parseInt(options.id)
    }
    this.data.userId = app.getUserId();
    this.loadData();
  },
  onShow: function (a) {
    // this.data.pageIndex = 1;
    // this.setData({
    //   serviceList: [],
    //   goodsList:  [],
    // })
    // this.loadData();
  },
  /**
   * 获取页面数据的 http请求
   */
  loadData: function () {
    var that = this
    a.httpPost('/product/type/list', {
      parent_id: 0,
      pageIndex: that.data.pageIndex,
      pageSize:  that.data.pageSize,
    }, function (res) {
      console.log('活动页页数据', res)
      if (res.data.code == 0) {
        if(res.data.totalcount > 0){
          that.data.pageIndex = that.data.pageIndex + 1;
        }

        that.data.typeId = res.data.data.typeId;
        that.setData({
          hasData: true,
          serviceList: res.data.data.types,
          goodsList:  res.data.data.products,
          banners:res.data.data.banners,
          isLoading: false, loading: true, netWork: true, dataIsLoad:true,
        });
      } else {
        that.setData({ isLoading: false, loading: true, netWork: true, dataIsLoad: false })
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, function () {
      that.setData({ isLoading: false, loading: true, netWork: true, dataIsLoad: false})
    })
  },
  goProductDetail(e){
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/product/detail/index?id=' + item.id,
    })
  },
  handleType(e){
    console.log('handleType',e)
    var type_id = e.currentTarget.dataset.id;
    console.log('type_id', type_id);
    this.data.typeId = type_id;
    this.data.pageIndex = 1
    this.setData({
      isLoading:true,
      goodsList:[],
    })
    this.onClickServiceType(type_id);
  },
  onClickServiceType: function (type_id) {
    var that = this;
    a.httpPost("/product/type/platform/product/list", {
      type_id:type_id,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      keyword:that.data.keyword,
    }, function (res) {
      console.log("获取商家服务类型返回数据:", res)
      if(res.data.code == 0){
        if(res.data.totalcount > 0){
          that.data.pageIndex = that.data.pageIndex + 1;
          var products = res.data.data.products;
          if(products.length > 0){
            for (var index in products) {
              that.data.goodsList.splice(that.data.goodsList.length, 0, products[index]);
            }
            that.setData({
              hasData: true,
              goodsList: that.data.goodsList
            })
          }
        }
      }
      
      that.setData({
        isLoading: false,
      })

    }, function (a) {
      console.log("获取商家服务类型返回数据 err:", a);
    });
  },
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.setData({
      sections:[],
      serviceList:[],
      goodsList:[],
    })
    this.loadData();
  },
  handleChange(e){
    console.log('handleChange',e)
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.onClickServiceType(this.data.typeId);
  },
});